//
//  YWHelper.m
//  faceTime
//
//  Created by 历成栋 on 2020/11/26.
//

#import "YWHelper.h"

static NSString * const DFB_SWX_KEY_YW_TOKEN = @"DFB_SWX_KEY_YW_TOKEN";

@interface YWHelper ()

@property (nonatomic, copy, readwrite) NSString *server;/**服务器地址*/

@property (nonatomic, copy, readwrite) NSString *logServer;/**日志上传的地址*/

@property (nonatomic, copy, readwrite) NSString *ywToken;/**云屋sdk的鉴权令牌*/

@property (nonatomic, copy, readwrite) NSString *ywnickName;/**云屋登陆的用户昵称*/

@property (nonatomic, copy, readwrite) NSString *ywUserId;/**代表用户唯一性*/

@property (nonatomic, copy, readwrite) NSString *ywUserAuthCode;/**默认填空，只有开启第三方认证才需要填写，（开启第三方认证时，云屋SDK服务器将连接提前配好的业务方服务器进行实时验证）*/

@property (nonatomic, copy, readwrite) NSString *ywCookie;/**自定义数据(在响应消息中回传给调用者)，不需要时传空字符串*/
///**唯一方法*/
@end

@implementation YWHelper
static YWHelper *shareInstance;
+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[self alloc] init];
    });
    
    return shareInstance;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        _server = @"";
        _logServer = @"";
        _ywnickName = @"";
        _ywUserId = @"";
        _ywUserAuthCode = @"";
        _ywCookie = @"";
    }
    return self;
}

-(void)readYWLoginInfoResultsDelegate:(id<YWHelperDelegate>)delegate{
    self.delegate = delegate;
    if (self.ywToken == nil||[self.ywToken  isEqualToString:@""]==YES) {
        DLog(@"去本地取token");
        self.ywToken = (NSString *)[self readLocalSomeThingForKey:DFB_SWX_KEY_YW_TOKEN];
    }
    if (self.ywToken!=nil &&[self.ywToken isEqualToString:@""]==NO) {
        if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(readYWLoginInfoResultsIsSuccess:)]) {
            [self.delegate readYWLoginInfoResultsIsSuccess:YES];
        }
        return;
    }
    DLog(@"去网络请求,并保存本地");
//  模拟网络请求获取token
    __block YWHelper *weakSelf = self;
    [self getywTokenFromServerWithBlock:^(NSString *ywToken) {
        if (weakSelf.delegate!=nil &&[weakSelf.delegate respondsToSelector:@selector(readYWLoginInfoResultsIsSuccess:)]) {
            [weakSelf.delegate readYWLoginInfoResultsIsSuccess:ywToken==nil?NO:YES];
        }
    }];
//    <#在此处设置登录所需要的数据信息#>
}

/**从服务端获取token,并在本地保存*/
-(void)getywTokenFromServerWithBlock:(void(^)(NSString *ywToken))block{
    __block YWHelper *weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        weakSelf.ywToken = @"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJRCI6IjdiaXkxamV1MjkiLCJleHAiOjE2MDY1NzQ3MDAsInNhbHQiOiI4RDdtTmd2c1ZKam5USCJ9.EsYfN8D34A4Q0irgYAn3qYFT2gASR0DXmEC8odgezLk";
        [weakSelf updateLocalYWToken: weakSelf.ywToken];
    });
}

-(void)updateLocalYWToken:(NSString *)ywToken{

    [self writeSomeThing:ywToken ofKey:DFB_SWX_KEY_YW_TOKEN];

}

-(void)writeSomeThing:(nullable id)obj ofKey:(nonnull NSString *)key{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:obj forKey:key];
    [userDefaults synchronize];
}

-(id)readLocalSomeThingForKey:(NSString *)key{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return  [userDefaults objectForKey:key];
}



@end
